package win.smartown.kotlin.grammar

/**
 * Created by smartown on 2017/6/5.
 *
 * 使用集合
 */

fun main(args: Array<String>) {
    testListIterate()
    testListIn()
    testListLambda()
}

//对一个集合进行迭代
fun testListIterate(): Unit {
    val items = listOf("apple", "banana", "kiwi")
    for (item in items) {
        print("$item ")
    }
    println()
}

//使用in操作符检查集合中是否包含某个对象
fun testListIn(): Unit {
    val items = listOf("apple", "banana", "kiwi")
    when {
        "orange" in items -> println("juicy")
        "apple" in items -> println("apple is fine too")
    }
}

//使用lambda表达式过滤和映射集合
fun testListLambda(): Unit {
    val fruits = listOf("banana", "avocado", "apple", "kiwi")
    fruits.filter { it.startsWith("a") }
            .sortedBy { it }
            .map(String::toUpperCase)
            .forEach { print("$it ") }
}